/*
 * UIUtils.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.ArrayRes;
import android.support.annotation.ColorRes;
import android.support.annotation.IntegerRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yidiandao.business.BusinessApplicationLike;

import java.util.List;

/**
 * UI工具类.
 */
public class UIUtils {

    private static Toast mToast;

    /**
     * 获得上下文
     */
    public static Context getContext() {
        return BusinessApplicationLike.getContext();
    }

    /**
     * 获得资源
     */
    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
     * 获得string类型的数据
     */
    public static String getString(@StringRes int resId) {
        return getContext().getResources().getString(resId);
    }

    public static String getString(@StringRes int resId, Object... formatArgs) {
        return getContext().getResources().getString(resId, formatArgs);
    }

    public static int getInt(@IntegerRes int resId) {
        return getContext().getResources().getInteger(resId);
    }

    /**
     * 获得数组集合
     */
    public static String[] getStringArray(@ArrayRes int resId) {
        return getResources().getStringArray(resId);
    }

    /**
     * 获得颜色值
     */
    public static int getColor(@ColorRes int resId) {
        return getResources().getColor(resId);
    }

    public static ColorStateList getColorStateList(@ColorRes int resId) {
        return getResources().getColorStateList(resId);
    }

    /**
     * 获得handler
     */
    public static Handler getMainHandler() {
        return BusinessApplicationLike.getHandler();
    }

    /**
     * 判断App是否在前台运行
     */
    public static boolean isForeground() {
        return ActivityStack.isForeground();
    }

    /**
     * 在主线程中执行任务
     */
    public static void post(Runnable task) {
        getMainHandler().post(task);
    }

    /**
     * 在主线程中执行任务
     *
     * @param delayMillis 延迟的时间
     */
    public static void postDelayed(Runnable task, long delayMillis) {
        getMainHandler().postDelayed(task, delayMillis);
    }

    /**
     * 移除任务
     */
    public static void removeCallbacks(Runnable task) {
        getMainHandler().removeCallbacks(task);
    }

    /**
     * 像素转dp
     */
    public static int px2dp(int px) {
        // dpi = squa(w*w + h*h)/ 对象线
        // px = dp * (dpi / 160)
        // dp = px * 160 / dpi

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int dpi = metrics.densityDpi;
        return (int) (px * 160f / dpi + 0.5f);
    }

    /**
     * dp转px
     */
    public static int dp2px(int dp) {
        // px = dp * (dpi / 160)
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int dpi = metrics.densityDpi;

        return (int) (dp * (dpi / 160f) + 0.5f);
    }

    public static float getDimens(int id) {
        return getResources().getDimension(id);
    }

    /**
     * 获得包名
     */
    public static String getPackageName() {
        return getContext().getPackageName();
    }

    /**
     * 设置状态栏颜色
     *
     * @param activity 需要设置的activity
     * @param color    状态栏颜色值
     */
    public static void setStateBarColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.getWindow().setStatusBarColor(color);
                return;
            }

            // 设置状态栏透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // 生成一个状态栏大小的矩形
            View statusView = createStatusView(activity, color);
            // 添加 statusView 到布局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(statusView);
            // 设置根布局的参数
            ViewGroup rootView =
                    (ViewGroup)
                            ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

    /**
     * 生成一个和状态栏大小相同的矩形条
     *
     * @param activity 需要设置的activity
     * @param color    状态栏颜色值
     * @return 状态栏矩形条
     */
    private static View createStatusView(Activity activity, int color) {
        // 获得状态栏高度
        int resourceId =
                activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);

        // 绘制一个和状态栏一样高的矩形
        View statusView = new View(activity);
        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusView.setLayoutParams(params);
        statusView.setBackgroundColor(color);
        return statusView;
    }

    /**
     * 使状态栏透明
     * <p>
     * <p>适用于图片作为背景的界面,此时需要图片填充到状态栏
     *
     * @param activity 需要设置的activity
     */
    public static void setStateBarTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
                return;
            }

            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 设置根布局的参数
            ViewGroup rootView =
                    (ViewGroup)
                            ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

    public static void showToast(String message) {
        if (TextUtils.isEmpty(message)) {
            return;
        }

        if (mToast == null) {
            mToast = Toast.makeText(getContext(), message, Toast.LENGTH_LONG);
        } else {
            mToast.setText(message);
        }
        mToast.show();
    }

    /**
     * @return 主线程的id
     */
    public static long getMainThreadId() {
        return BusinessApplicationLike.getPid();
    }

    /**
     * @return 主线程的Handler
     */
    public static Handler getMainThreadHandler() {
        return BusinessApplicationLike.getHandler();
    }

    /**
     * 安全的加载数据
     */
    public static void postTaskSafely(Runnable task) {
        // 当前线程==子线程,通过消息机制,把任务交给主线程的Handler去执行
        // 当前线程==主线程,直接执行任务
        long currentId = android.os.Process.myTid();
        if (currentId == getMainThreadId()) {
            //直接执行
            task.run();
        } else {
            //交给主线程执行
            getMainThreadHandler().post(task);
        }
    }

    /**
     * 判断应用是否已经启动
     *
     * @param context 一个context
     */
    public static boolean isAppAlive(Context context) {
        ActivityManager activityManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos =
                activityManager.getRunningAppProcesses();
        for (int i = 0; i < processInfos.size(); i++) {
            if (processInfos.get(i).processName.equals(getPackageName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 动态获取ListView的ScrollY值
     */
    public static int getListViewScrollY(ListView listView) {
        View childAt = listView.getChildAt(0);
        if (childAt == null) {
            return 0;
        }
        int headerHeight = 0;

        int firstVisiblePosition = listView.getFirstVisiblePosition();
        if (firstVisiblePosition >= 1) {
            headerHeight = listView.getHeight();
        }
        int top = childAt.getTop();
        return -top + firstVisiblePosition * childAt.getHeight() + headerHeight;
    }

    public static void setTextDrawableLeft(TextView view, int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
        view.setCompoundDrawables(drawable, null, null, null); //画在左边
    }

    public static double change(double a) {
        return a * Math.PI / 180;
    }
}
